home *** CD-ROM | disk | FTP | other *** search
- /*
- * event.c - startup, stopping, and the main event loop
- */
-
- #include <quickdraw.h>
- #include <memory.h>
- #include <font.h>
- #include <window.h>
- #include <dialog.h>
- #include <osutil.h>
- #include <menu.h>
- #include <event.h>
- #include <textedit.h>
- #include <desk.h>
- #include <control.h>
- #include <toolutil.h>
-
- #define DATA
- #include "tablut.h"
-
- /*
- * main() - start the program.
- */
- main()
- {
- static WindowRecord wrecord; /* Storage for window record. */
-
- /*
- * Initialize our program. It seems best to handle:
- * Memory inits first, ToolBox inits second, then the program variables'
- * inits. Note that the order of inits is important; see "Using the
- * Dialog Manager" in the Dialog Mgr section.
- */
-
- setupmemory();
- InitGraf(&thePort); InitFonts(); InitWindows(); InitMenus(); TEInit();
- InitDialogs((ProcPtr) 0); InitCursor();
- SetEventMask(everyEvent - keyUpMask); /* set default event mask */
-
- /*
- * Get the port which is the whole screen, to use when deactivating
- * our window. This prevents the current grafPort pointer from
- * ever dangling.
- */
- GetWMgrPort(&screenport);SetPort(screenport);
-
- if (seeanyfiles()) { /* read the finder file info */
- if (printanyfiles()) { /* print requested files & exit */
- return;
- }
- }
-
- /*
- * GetNewWindow posts an update event for the new window,
- * so it will be redrawn right away. (-1 is the frontmost window)
- * Set the default text drawing for our window.
- */
- mywindow = GetNewWindow(WINDOWID, &wrecord, (long) -1);
- SetPort(mywindow); TextFont(systemFont);
-
- setupmenus(); /* pull in and setup our menus */
- getboard(); /* init the board picture */
- getstate(); /* init the game status strings */
- makegrid(); /* create the representation of the board */
- getpieces(); /* init the playing pieces' pictures */
- makehomes(); /* create the pieces' home locations */
- getcontrols(); /* init the game controls */
- getcursors(); /* init the game's cursors */
- initmoves(); /* init the recording of games */
- drawwindow(); /* draw everything that has been set up */
- if (!openanyfiles()) { /* read the requested game or... */
- newgame(); /* ...start a new one */
- }
- while (doevent()) /* do the event-driven part of the program */
- ;
- closeup();
- }
-
- /*
- * closeup() - prepare to exit the program. (called before exiting).
- * Restore the state of the machine so the finder will be happy.
- */
- closeup()
- {
- InitCursor();
- SetEventMask(everyEvent - keyUpMask);
- }
-
- /*
- * doevent() - one loop through the main event handler
- * Returns 1 if the user wants to continue; 0 if it's time to quit
- */
- int
- doevent()
- {
- GrafPtr saveport;
- EventRecord myevent;
- WindowPtr whichwindow; /* Points to window of MouseDown */
- DialogPtr whichdialog; /* dialog of MouseDown */
- short whichitem; /* mouse's dialog item */
- int windowcode; /* What mouse was in when event posted. */
- ControlHandle whichcontrol; /* the control that was pressed */
- short partcode; /* the pressed part of that control */
- Point pt; /* temp point */
- KeyMap keys; /* temp key bitlist */
- int pieceid; /* index of the selected piece (-1 if none) */
- Point newgrid; /* board grid coords of piece's destination */
- int userdone; /* True when user wants to exit program */
-
- SystemTask(); /* give the system a moment */
- userdone = 0;
-
- /*
- * Track the realtime objects: the cursor type.
- * If we had a clipboard, we'd update its window here.
- * If we had crosshairs (like MacDraw), we'd update them here.
- */
-
- GetMouse(&pt); GetKeys(keys); fixcursor(&pt, keys);
-
- if (!GetNextEvent(everyEvent, &myevent)) {
- (void) IsDialogEvent(&myevent); /* blinks the edit point */
- } else if ((myevent.modifiers & cmdKey) &&
- (myevent.what == keyDown || myevent.what == autoKey)) {
- /* handle command keys here -- dialogs can't. */
- userdone = docommand(MenuKey(
- (char) (myevent.message & charCodeMask)));
- } else if (IsDialogEvent(&myevent)) {
- if (DialogSelect(&myevent, &whichdialog, &whichitem)) {
- /* if we had modeless dialogs, we'd handle their events here */
- }
- } else {
- switch (myevent.what) {
- case mouseDown:
- windowcode = FindWindow(pass(myevent.where), &whichwindow);
- switch (windowcode) {
- case inSysWindow: /* a DA's window */
- SystemClick(&myevent, whichwindow);
- break;
- case inMenuBar: /* some other menu */
- userdone = docommand(MenuSelect(pass(myevent.where)));
- break;
- case inGoAway: /* GoAway region */
- if (TrackGoAway(whichwindow, pass(myevent.where))) {
- /* if we could close our window, we'd do it here */
- }
- break;
- case inDrag: /* Title Bar */
- if (whichwindow != FrontWindow() &&
- (myevent.modifiers & cmdKey) == 0) {
- SelectWindow(whichwindow);
- }
- /* If we could drag our window, we'd do it here */
- break;
- case inGrow: /* Grow Region */
- /*
- * because we don't have a grow region, we treat it
- * as part of the window's content.
- */
-
- case inContent: /* Somewhere in the window */
- if (whichwindow != FrontWindow()) {
- /* the window isn't active - activate it */
- SelectWindow(whichwindow);
- } else {
- /* the window is active, use it */
- GlobalToLocal(&myevent.where);
- if (whichwindow == mywindow) {
- if ((partcode = FindControl(pass(myevent.where),
- whichwindow, &whichcontrol))) {
- docontrol(whichcontrol, partcode,
- &myevent.where);
- } else if ((pieceid =
- findpiece(&myevent.where)) >= 0) {
- dragpiece(pieceid, &myevent.where);
- newgrid.h =
- piece[pieceid].prevlook.bounds.left
- + piece[pieceid].class->etoc.h;
- newgrid.v =
- piece[pieceid].prevlook.bounds.top
- + piece[pieceid].class->etoc.v;
- pointtogrid(&newgrid);
- movepiece(pieceid, &newgrid);
- }
- }
- }
- break;
- }
- break;
- case keyDown:
- case autoKey:
- /* if the user could type, here's where we'd handle that. */
- break;
- case updateEvt: /* update the appropriate window */
- if ((WindowPtr)(myevent.message) == mywindow) {
- drawwindow();
- }
- break;
- case activateEvt: /* activate or deactivate a window */
- if ((WindowPtr) (myevent.message) == mywindow) {
- if (myevent.modifiers & 1) { /* activate */
- SetPort(mywindow);
- actmenus();
- } else { /* deactivate */
- SetPort(screenport);
- deactmenus();
- }
- }
- }
- }
- return(!userdone);
- }
-